/**
 * Copyright ©2012-2018 HopeHaiyang Corporation, All Rights Reserved
 * http://www.wuhanhope.com
 */
package hope.platform.data.server.data.img;

import hope.platform.data.dao.EmergencyRepository;
import hope.platform.data.domain.Emergency;
import hope.platform.data.server.data.header.EmergencyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Optional;

/**
 * class info
 *
 * @author hujia
 *         Create at 2018/7/25 11:00
 */
@Service
public class EmergencyServiceImg implements EmergencyService {

    @Autowired
    private EmergencyRepository repository;

    @Override
    public Emergency addEmergency(Emergency emergency) {
        emergency.setGmtCreate(new Date());
        emergency.setGmtModified(new Date());
        return repository.save(emergency);
    }

    @Override
    public Emergency updateEmergency(Emergency emergency) {
        Optional<Emergency> tmp = repository.findById(emergency.getId());
        if (tmp.isPresent()) {
            if (emergency.getTitle() != null) {
                tmp.get().setTitle(emergency.getTitle());
            }
            if (emergency.getContent() != null) {
                tmp.get().setContent(emergency.getContent());
            }
            tmp.get().setGmtModified(new Date());
            return repository.save(tmp.get());
        }
        return null;
    }

    @Override
    public Page<Emergency> findAll(Pageable pageable) {
        return repository.findAll(pageable);
    }

    @Override
    public Page<Emergency> findByUserName(String userName, Pageable pageable) {
        return repository.findByUserName(userName,pageable);
    }
}
